package hot100.h0912;

import java.util.*;
import java.util.stream.Collectors;

/**
  * @description 找到第一个不重复的字符
  * @author 不知名帅哥
  * @date 2024/9/12 23:11
  * @version 1.0
*/
public class FindFirstNoDuplicateCharacter {
    public static void main(String[] args) {
        String s="aabb";
        System.out.println(getRes(s));
    }
    public static int getRes(String s){
        HashMap<Character, Integer> map = new HashMap<>();
        HashSet<Character> set = new HashSet<>(); //重复名单
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (set.contains(c)) continue;
            if (map.containsKey(c)){
                map.remove(c);
                set.add(c);
            }else {

                map.put(c,i);
            }
        }
        if (map.size()==0) return -1;
        List<Character> collect = map.keySet().stream().sorted(new Comparator<Character>() {
            @Override
            public int compare(Character o1, Character o2) {
                return map.get(o1) - map.get(o2);
            }
        }).collect(Collectors.toList());

        return map.get(collect.get(0));
    }

    public static int getRes2(String s){
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            map.put(c,map.getOrDefault(c,0)+1);
        }
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.get(c)==1){
                return i;
            }
        }
        return -1;
    }
}
